{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Object"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{admonition} 核心功能\n",
    "`Object` 是 TVM 运行时所有对象的基类，主要提供以下核心能力：\n",
    "- 类型系统：通过类型索引(`type_index`)实现运行时类型识别(RTTI)\n",
    "- 引用计数：管理对象生命周期\n",
    "- 内存管理：通过自定义删除器控制对象销毁\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "````{admonition} 类型相关属性\n",
    "\n",
    "```cpp\n",
    "static constexpr const char* _type_key = \"runtime.Object\";\n",
    "static constexpr uint32_t _type_index = TypeIndex::kDynamic;\n",
    "```\n",
    "- `_type_key`：类型唯一标识符（如 `\"runtime.NDArray\"`）\n",
    "- `_type_index`：类型索引（`kDynamic` 表示动态分配）\n",
    "````"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "````{admonition} 引用计数器\n",
    "\n",
    "```cpp\n",
    "#if TVM_OBJECT_ATOMIC_REF_COUNTER\n",
    "  using RefCounterType = std::atomic<int32_t>;\n",
    "#else\n",
    "  using RefCounterType = int32_t; \n",
    "#endif\n",
    "```\n",
    "- 支持原子/非原子计数器选择\n",
    "- 控制对象是否支持多线程共享\n",
    "````"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "````{admonition} 类型继承控制\n",
    "```cpp\n",
    "static constexpr bool _type_final = false;\n",
    "static constexpr uint32_t _type_child_slots = 0;\n",
    "static constexpr bool _type_child_slots_can_overflow = true;\n",
    "```\n",
    "- `_type_final`：是否允许继承\n",
    "- `_type_child_slots`：为子类预留的类型槽位\n",
    "- `_type_child_slots_can_overflow`：是否允许超量子类数量\n",
    "````"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用示例：\n",
    "```cpp\n",
    "// 创建对象示例\n",
    "class MyObject : public Object {\n",
    " public:\n",
    "  int my_data;\n",
    "  \n",
    "  static constexpr const char* _type_key = \"custom.MyObject\";\n",
    "  TVM_DECLARE_FINAL_OBJECT_INFO(MyObject, Object);\n",
    "};\n",
    "\n",
    "// 注册类型（需在.cc文件中）\n",
    "TVM_REGISTER_OBJECT_TYPE(MyObject);\n",
    "\n",
    "// 使用示例\n",
    "ObjectRef obj_ref = make_object<MyObject>();\n",
    "const MyObject* ptr = obj_ref.as<MyObject>();\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{admonition} 设计亮点 \n",
    "1. 高效类型检查：通过预分配的 `type_index` 实现 O(1) 时间复杂度类型判断 \n",
    "2. 灵活继承：通过 `_type_child_slots` 在类型安全与性能之间取得平衡 \n",
    "3. 线程安全：可配置的原子计数器支持多线程场景 \n",
    "4. 内存控制：自定义删除器支持特殊内存分配策略  \n",
    "\n",
    "该设计为 TVM 的异构计算场景提供了高效的对象管理基础，特别是在需要频繁跨语言边界传递对象时（如 Python/C++ 交互），能保证类型安全和内存安全。\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "name": "C++14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
